package com.springBoot.springBootSysCore.config.task.quartz;


import java.util.Map;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.StringUtils;

import com.springBoot.springBootSysCore.common.utils.SpringContextHolder;


/**
 * 带状态的job
 * @author 
 * @version 1.0
 */
@DisallowConcurrentExecution
public class QuartzStatefulJobBean implements Job{
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException{
        String targetBeanId = (String) context.getMergedJobDataMap().get("serviceTag");
        if (StringUtils.isEmpty(targetBeanId)){
        	 return;
        }
        Object targetBean=SpringContextHolder.getApplicationContext().getBean(targetBeanId);
//        Object targetBean = SpringHelper.getBean(targetBeanId);
        if (null == targetBean){
        	return;
        }
        if (!(targetBean instanceof QuartzJob)){
        	return;
        }
        Map<?, ?> args = (Map<?, ?>) context.getMergedJobDataMap().get("args");
        QuartzJob job = (QuartzJob) targetBean;
        try{
            job.execute(args);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
